<!-- 商品详情的立即购买的确认页面 -->
<template>
	<view class="page">
		<!-- 地址 -->
		<view style="height: 80px;" class="address-list1" >
			<view class="list" @click="onSkip()">
				<view class="name-phone">
					<view class="name">
						<text class="one-omit">{{userName}}</text>
					</view>
					<view class="phone">
						<text>{{userPhone}}</text>
					</view>
				</view>
				<view class="address-edit">
					<view class="address">
						<text>{{userAddress}}</text>
					</view>
				</view>
			</view>
		</view>
		<!-- 商品 -->
		<view class="goods-data">
			<view class="goods-title">
				<text>商品信息</text>
			</view>
			<!-- 秒杀情况 -->
			<view v-if="flag === true" class="goods-list">
				<view class="list" :key="index">
					<view class="thumb">
						<image :src="goodsList.img" mode=""></image>
					</view>
					<view class="item">
						<view class="title">
							<text class="name one-omit">{{goodsList.goodsName}}</text>
						</view>
						<view class="price-number">
							<view class="price">
								<text class="min">￥{{goodsList.seckillPrice}}</text>
							</view>
							<view class="number">
								<text>x {{killNum}}</text>
							</view>
						</view>
					</view>
				</view>
			</view>
			<!-- 购物车情况 -->
			<view v-else="flag === false" class="goods-list">
				<view class="list" v-for="(item,index) in goodsList" :key="index">
					<view class="thumb">
						<image :src="ip+item.img" mode=""></image>
					</view>
					<view class="item">
						<view class="title">
							<text class="name one-omit">{{item.goodsName}}</text>
						</view>
						<view class="price-number">
							<view class="price">
								<text class="min">￥{{item.buyPrice}}</text>
							</view>
							<view class="number">
								<text>x {{item.goodsNum}}</text>
							</view>
						</view>
					</view>
				</view>
			</view>
			<view class="delivery">
				<div class="list">
					<view class="title">留言</view>
					<view class="content" style="height: 50px;">
						<text style="color: darkgray;">最多输入100字</text><br>
					</view>
				</div>
				<input type="text" v-model="input" placeholder="请输入备注信息">
			</view>
		</view>
		<!-- 优惠 -->
		<view class="discounts-data">
			<view v-if="flag == false" class="discounts">
				<div class="list" @click="$refs['UseCoupon'].show(this.couponInfo.CouponNumber)">
					<view class="title">优惠券</view>
					<view class="content">
						<text v-if="isUse" style="color: red;">-￥{{coupon}}.00</text>
						<text v-else class>{{coupon}}</text>
						<text class="iconfont icon-more"></text>
					</view>
				</div>
			</view>
			<view v-else="flag == true" class="discounts">
				<view class="title">秒杀商品不可以使用优惠券！</view>
			</view>
		</view>
		<!-- 订单金额 -->
		<view class="order-price">
			<!-- 购物车情况 -->
			<view v-if="flag == false" class="price-list">
				<view class="list">
					<view class="title">
						<text>商品金额</text>
					</view>
					<view class="price">
						<text>￥{{goodsPrice}}</text>
					</view>
				</view>
				<view class="list">
					<view class="title">
						<text>配送费</text>
					</view>
					<view class="price">
						<text class="highlight">+￥{{deliveryFee}}</text>
					</view>
				</view>
			</view>
			<!-- 秒杀情况 -->
			<view v-else="flag == true" class="price-list">
				<view class="list">
					<view class="title">
						<text>商品金额</text>
					</view>
					<view class="price">
						<text>￥{{goodsList.seckillPrice}}</text>
					</view>
				</view>
			</view>
		</view>
		<!-- 地址提示 -->
		<view class="address-tips" :style="scrollTop >= 100 ? '':'display:none'">
			<text>{{address}}</text>
		</view>
		<!-- 底部合计提交 -->
		<view class="footer-submit">
			<!-- 秒杀情况 -->
			<view v-if="flag == true" class="price">
				<text class="min">￥{{goodsList.seckillPrice}}</text>
			</view>
			<!-- 购物车情况 -->
			<view v-else="flag == false" class="price">
				<text class="min">￥{{price}}</text>
			</view>
			<view class="submit" @click="onSubmit">
				<text>提交订单</text>
			</view>
		</view>
		<!-- 发票 -->
		<invoice-info ref="InvoiceInfo"></invoice-info>
		<!-- 优惠券 -->
		<UseCoupon :objdata="objdata" ref="UseCoupon"> </UseCoupon>
	</view>
</template>

<script>
	import InvoiceInfo from '../../components/InvoiceInfo/InvoiceInfo.vue';
	import UseCoupon from '../../components/UseCoupon/UseCoupon.vue';
	import {
		baseUrl
	} from '@/config/baseUrl.js';
	export default {
		components: {
			// 发票
			InvoiceInfo,
			// 优惠券
			UseCoupon,
		},

		data() {
			return {
				objdata: {
					type: '',
				},
				couponInfo: {
					couponPrice: '',
					CouponId: '',
					CouponNumber: '',
				},
				coupon: '去使用',
				scrollTop: 0,
				goodsList: {},
				userAddress: '', //用户地址
				addressId: '', //地址id
				userName: '', //用户姓名
				userPhone: '', //用户手机号
				killNum: 1, //秒杀数量
				flag: '', //秒杀判断
				price: 0, //总金额
				goodsPrice: "", //总金额
				fastigium: '', //高峰期状态判断
				deliveryFee: 0, //配送费
				couponItem: 0, //优惠券面值
				isUse: false,
				date: new Date().toISOString(),
				input: '',
ip:"http://43.142.173.112:9090/images/",
			};
		},
		onPageScroll(e) {
			this.scrollTop = e.scrollTop;
		},
		onUnload() {
			uni.$off('onConfirm')
		},
		onLoad(params) {
			
			uni.$on('onConfirm', (data) => {
				this.couponInfo = JSON.parse(data)
				if (this.couponInfo.couponPrice != "") {
					this.coupon = this.couponInfo.couponPrice;
					this.couponItem = this.couponInfo.couponPrice;
					this.isUse = true;
					this.price -= this.couponInfo.couponPrice;
				} else {
					this.price += this.couponItem;
					this.coupon = '去使用';
					this.couponInfo.CouponNumber = -1;
					this.isUse = false;
				}

			})
			let _this = this;
			setInterval(function() {
				_this.date = Date.parse(new Date());
			}, 1000);
			var list = decodeURIComponent(params.goodsList || '');
			this.goodsList = JSON.parse(list);
			this.goodsPrice =  decodeURIComponent(params.price || '');
			this.getData();
		},
		methods: {
			getData() {
				uni.request({
					url: baseUrl + "/userOrder/getUserInfo",
					method: 'POST',
					data: {
						userId: uni.getStorageSync("userId"),
						shopsId: 1
					},
					header: {
						"Content-Type": "application/x-www-form-urlencoded"
					},
					success: (res) => {
						console.log(res)
						this.userName = res.data.userInfo.userName;
						this.userAddress = res.data.userInfo.userMapAddress + res.data.userInfo.address;
						this.addressId = res.data.userInfo.addressId;
						this.userPhone = res.data.userInfo.userPhone;
						this.fastigium = res.data.fastigium;
						if (this.fastigium) {
							this.deliveryFee = res.data.deliveryFee;
							this.price += this.deliveryFee;
						}
						this.flag = uni.getStorageSync("kill")
						if (!this.flag) {
							for (var i = 0; i < this.goodsList.length; i++) {
								this.price += this.goodsList[i].buyPrice;
							}
							uni.setStorageSync("price", this.price)
						}
					}
				})
			},
			dateFormat(time) {
				let date = new Date(time);
				let year = date.getFullYear();
				// 在日期格式中，月份是从0开始的，因此要加0，使用三元表达式在小于10的前面加0，以达到格式统一  如 09:11:05
				let month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
				let day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
				let hours = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
				let minutes = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
				let seconds = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
				// 拼接
				return year + month + day + hours + minutes + seconds;
			},
			/**
			 * 提交订单
			 */
			onSubmit() {
				let goodsId = ""
				let goodsNum = ""
				for (let i = 0; i < this.goodsList.length; i++) {
					if (i + 1 == this.goodsList.length) {
						goodsId += this.goodsList[i].goodsId
						goodsNum += this.goodsList[i].goodsNum
					} else {
						goodsId += this.goodsList[i].goodsId + ","
						goodsNum += this.goodsList[i].goodsNum + ","
					}
				};
				var data = {
					couponId: this.couponInfo.CouponId,
					// shopsId: uni.getStorageSync("shopsId"),
					userId: uni.getStorageSync("userId"),
					shopsId: 1,
					orderTime: this.dateFormat(this.date),
					orderNo: this.dateFormat(this.date) + uni.getStorageSync("userId"),
					addressId: this.addressId,
					money: this.price,
					deliveryFee: this.deliveryFee,
					remarks: this.input,
					goodsId: goodsId,
					goodsNum: goodsNum,
				};
				//创建订单
				uni.request({
					url: baseUrl + "/userOrder/createOrder",
					method: 'POST',
					data: data,
					header: {
						"Content-Type": "application/x-www-form-urlencoded"
					},
					success: (res) => {
						uni.navigateTo({
							url: '/pages/CashierDesk/CashierDesk?orderNo=' + data.orderNo + '&money=' +
								data.money
						});
					}
				});
			},

			/**
			 * 跳转点击
			 * @param {String} type 跳转类型
			 */
			onSkip() {
				console.log("asdasd")
				uni.navigateTo({
					url: '/pages/UserAddress/UserAddress',
				})
			},
			address(table){
				this.userName =table.userName;
				this.userAddress =table.userMapAddress + table.address;
				this.addressId = table.addressId;
				this.userPhone = table.userPhone;
			}
		}
	}
</script>

<style scoped lang="scss">
	@import 'ConfirmOrder.scss';
</style>
